/**
 *   Measure idle time / activity time 
 *
 *	   When there is activity, call the registerActivity() method
 *
 */
public class ActivityTracker 
{
	// If this amount of time passes with no activity, assume the person left the computer
	public static final long PERSON_LEFT_COMPUTER_TIMEOUT_MS = 1000l * 60l * 15l;  // 15 minutes
	
	long startOfActivityMs = -1L;
	long lastActivityMs = -1L;
	
	public ActivityTracker()
	{
		reset();
	}
	
	public void registerActivity()
	{
		if(getIdleTimeMs() > PERSON_LEFT_COMPUTER_TIMEOUT_MS)
		{
			reset();
		}
		else
		{
			lastActivityMs = System.currentTimeMillis();
		}
	}
	
	public void reset()
	{
		startOfActivityMs = System.currentTimeMillis();
		lastActivityMs = System.currentTimeMillis();
	}
	
	/**
	 * @return  Time since last activity
	 */
	public long getIdleTimeMs()
	{
		long elapsedSinceLastActivityMs = System.currentTimeMillis() - lastActivityMs;
		return elapsedSinceLastActivityMs;
	}
	
	/**
	 * @return  Time active since tracking was last reset.
	 */
	public long getActiveTimeMs()
	{
		checkForAbandonment();
		
		long activeMs = System.currentTimeMillis() - startOfActivityMs;
		return activeMs;
	}
	
	private void checkForAbandonment()
	{
		if(getIdleTimeMs() > PERSON_LEFT_COMPUTER_TIMEOUT_MS)
		{
			reset();
		}
	}
	
}
